package com.test.domain;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.test.bean.MemStatusType;
import com.test.bean.MemType;
import com.test.bean.PapaMem;

public class PapaGoDAO {

	public static void delete(int memId) {
		Session session = DefaultFactory.getSessionFactory().openSession();
		//Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction(); // 要寫交易才會開始
		Query query = session
				.createQuery("delete PapaMem where memId = :memyaId ");
		query.setParameter("memyaId", memId);
		PapaMem bean = new PapaMem();
		query.executeUpdate();
		session.delete(bean);
		tx.commit();
		session.close();
		System.out.println(" done.");
	}
	
	
	public static void selectById(int memId) {
		Session session = DefaultFactory.getSessionFactory().openSession();
		Query query = session
				.createQuery("from PapaMem where memId = :memyaId ");
		query.setInteger("memyaId", memId);
		List<?> users = query.list();
		Iterator<?> list = users.iterator();
		while (list.hasNext()) {
			PapaMem item = (PapaMem) list.next();
			System.out.println(item.getSex());
			System.out.println(item.getAccount());
			System.out.println(item.getMemStatusType().getMemStatus());
			System.out.println(item.getMemType().getMemType());
		}
		session.close();
		System.out.println("done.");
	}

	public static void selectAll() {
		Session session = DefaultFactory.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(PapaMem.class);
		Iterator<?> users = criteria.list().iterator();
		while (users.hasNext()) {
			PapaMem item = (PapaMem) users.next();
			
			System.out.println(item.getMemId());
			System.out.println(item.getSex());
			System.out.println(item.getAccount());
			System.out.println(item.getMemStatusType().getMemStatus());
			System.out.println(item.getMemType().getMemType());
		}
		session.close();
		System.out.println("done.");
		DefaultFactory.shutdown();
	}
	
	public static int insert() {
		Session session = DefaultFactory.getSessionFactory().openSession();
		//Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		PapaMem bean = new PapaMem();
		MemStatusType status= new MemStatusType();
		status.setMemstatusId(1);
		MemType type=new MemType();
		type.setMemtypeId(2);
		
		bean.setAccount("Q");
		bean.setName("C");
		bean.setMemPassword("F");
		bean.setBirth(new java.util.Date());
		bean.setEmail("AA");
		bean.setSex("男");
		// 無圖片
		bean.setBirth(new java.util.Date());
		bean.setPhone("023232323");
		bean.setMemAddress("台北市");
		bean.setMemStatusType(status);
		bean.setMemType(type);
		bean.setMemDate(new Timestamp(System.currentTimeMillis()));

		int result=(Integer) session.save(bean);
		tx.commit();
		session.close();
		return  result;
	}
	

	public static void update() {
		Session session = DefaultFactory.getSessionFactory().openSession();
		//Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		PapaMem bean = new PapaMem();
		bean.setMemId(10);
		bean.setAccount("S");
		bean.setName("C");
		bean.setMemPassword("F");
		bean.setBirth(new java.util.Date());
		bean.setEmail("AA");
		bean.setSex("無");
		// 無圖片
		bean.setBirth(new java.util.Date());
		bean.setPhone("023232323");
		bean.setMemAddress("台北市");
		
		bean.setMemDate(new Timestamp(System.currentTimeMillis()));

		session.update(bean);
		tx.commit();
		session.close();
	}
	
	
	public static void main(String[] args) {
		//selectAll();
		//selectById(2);
		// QueryAssociation(2);
		//delete(8);
		//insert();
		update();
	}

}
